package gui.ClassroomMenu;

import gui.ClassroomMenu.exceptions.NoSelection;
import gui.ClassroomMenu.exceptions.NothingSelectedException;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;

import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.ListModel;

import model.basics.ClassRoom;
import model.basics.Resource;
import org.jdesktop.application.Application;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ClassroomPanel extends javax.swing.JPanel {
	private JTabbedPane classroomTabbedPanel;
	private JPanel informationPanel;
	private JPanel resourcesPanel;
	private JButton backButton;
	private JButton modifyButton;
	private JList resourcesList;
	private JScrollPane resScrollPane;
	private JList classroomList;
	private JScrollPane jScrollPane;
	private JButton dropButton;
	private JButton addButton;
	private JTextArea statusTextArea;
	private JTextArea idTextArea;
	private JLabel statusLabel;
	private JLabel idLabel;
	private JTextArea maximunCapacityTextArea;
	private JLabel maximunCapacityLabel;
	private JFrame frame;

	public ClassroomPanel() {
		super();
		this.setPreferredSize(new java.awt.Dimension(666, 335));
		initGUI();
	}

	private void initGUI() {
		frame = new JFrame(); // se crea el frame
		frame.getContentPane().add(this);
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setTitle("Classroom Menu");
		frame.setVisible(true);
		try {
			// this.setPreferredSize(new java.awt.Dimension(666, 300));
			this.setLayout(null);
			{
				classroomTabbedPanel = new JTabbedPane();
				this.add(classroomTabbedPanel);
				classroomTabbedPanel.setBounds(31, 26, 291, 217);
				{
					informationPanel = new JPanel();
					classroomTabbedPanel.addTab("Information", null,
							informationPanel, null);
					informationPanel.setLayout(null);
					{
						maximunCapacityLabel = new JLabel();
						informationPanel.add(maximunCapacityLabel);
						maximunCapacityLabel.setText("Maximun Capacity");
						maximunCapacityLabel.setBounds(12, 12, 117, 14);
					}
					{
						maximunCapacityTextArea = new JTextArea();
						informationPanel.add(maximunCapacityTextArea);
						maximunCapacityTextArea.setBounds(147, 10, 127, 18);
						maximunCapacityTextArea.setEditable(false);
					}
					{
						idLabel = new JLabel();
						informationPanel.add(idLabel);
						idLabel.setText("Id");
						idLabel.setBounds(12, 58, 45, 14);
					}
					{
						statusLabel = new JLabel();
						informationPanel.add(statusLabel);
						statusLabel.setText("Status");
						statusLabel.setBounds(12, 100, 86, 14);
					}
					{
						idTextArea = new JTextArea();
						informationPanel.add(idTextArea);
						idTextArea.setBounds(147, 54, 127, 18);
						idTextArea.setEditable(false);
					}
					{
						statusTextArea = new JTextArea();
						informationPanel.add(statusTextArea);
						statusTextArea.setBounds(147, 100, 127, 18);
						statusTextArea.setEditable(false);
					}
				}
				{
					resourcesPanel = new JPanel();
					classroomTabbedPanel.addTab("Resources", null,
							resourcesPanel, null);
					resourcesPanel.setLayout(null);
					{
						resScrollPane = new JScrollPane();
						resourcesPanel.add(resScrollPane);
						resScrollPane.setBounds(12, 12, 262, 167);
						{
							ListModel resourcesListModel = new DefaultComboBoxModel(
									new String[] {});
							resourcesList = new JList();
							resScrollPane.setViewportView(resourcesList);
							resourcesList.setModel(resourcesListModel);
							resourcesList.setBorder(BorderFactory
									.createTitledBorder("Resource: quantity"));
							resourcesList
									.setPreferredSize(new java.awt.Dimension(
											259, 164));
						}
					}
				}
			}
			{
				addButton = new JButton();
				this.add(addButton);
				addButton.setText("Add New");
				addButton.setToolTipText("Add a new Classroom");
				addButton.setBounds(99, 261, 125, 19);
				addButton.addActionListener(new AddCR());
			}
			{
				dropButton = new JButton();
				this.add(dropButton);
				dropButton.setText("Drop one");
				dropButton.setToolTipText("Remove the selected classroom");
				dropButton.setBounds(429, 261, 125, 19);
				dropButton.addActionListener(new DropCR());
			}
			{
				jScrollPane = new JScrollPane();
				this.add(jScrollPane);
				jScrollPane.setBounds(363, 26, 291, 217);
				{
					ListModel classroomListModel = new DefaultComboBoxModel(
							new String[] {});
					classroomList = new JList();
					jScrollPane.setViewportView(classroomList);
					classroomList.setModel(classroomListModel);
					classroomList
							.setListData((Vector<ClassRoom>) model.search.System
									.getClassRoomGeneralList());
					classroomList
							.addListSelectionListener(new ClassroomListener());
					classroomList.setBorder(BorderFactory
							.createTitledBorder("Classrooms' names"));
				}
			}
			{
				modifyButton = new JButton();
				this.add(modifyButton);
				modifyButton.setText("Modify");
				modifyButton.setToolTipText("Modify the seleceted classroom");
				modifyButton.setBounds(261, 260, 125, 21);
				modifyButton.setName("modifyButton");
				modifyButton.addActionListener(new ModifCR());
			}

			{
				backButton = new JButton();
				this.add(backButton);
				backButton.setBounds(554, 303, 87, 21);
				backButton.setName("backButton");
				backButton.setText("Back");
				backButton.setToolTipText("Back to main menu");
				backButton.addActionListener(new Back());
			}
			Application.getInstance().getContext().getResourceMap(getClass())
					.injectComponents(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public class AddCR implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			new AddClassroom(classroomList);
			resourcesList.setListData(new Vector<Resource>());
			maximunCapacityTextArea.setText("");
			idTextArea.setText("");
			statusTextArea.setText("");
		}

	}

	public class ModifCR implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				if (classroomList.isSelectionEmpty())
					throw new NothingSelectedException();
				else {
					new ModifClassroom(classroomList, (ClassRoom) classroomList
							.getSelectedValue());
					resourcesList.setListData(new Vector<Resource>());
					maximunCapacityTextArea.setText("");
					idTextArea.setText("");
					statusTextArea.setText("");
				}
			} catch (NothingSelectedException e1) {
				new NoSelection();
			}
		}
	}

	public class DropCR implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				if (classroomList.isSelectionEmpty())
					throw new NothingSelectedException();
				else {
					model.search.System.getClassRoomGeneralList().remove(
							(ClassRoom) classroomList.getSelectedValue());
					classroomList
							.setListData((Vector<ClassRoom>) model.search.System
									.getClassRoomGeneralList());
					resourcesList.setListData(new Vector<Resource>());
					maximunCapacityTextArea.setText("");
					idTextArea.setText("");
					statusTextArea.setText("");
				}
			} catch (NothingSelectedException e1) {
				new NoSelection();
			}
		}
	}

	public class Back implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			// actually this button must return to main menu
			frame.dispose();
		}
	}

	public class ClassroomListener implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent arg0) {
			//generates the visual information
			if (classroomList.hasFocus()) {
				ClassRoom cr = (ClassRoom) classroomList.getSelectedValue();
				maximunCapacityTextArea.setText(((Integer) cr.getMaxCapacity())
						.toString());
				idTextArea.setText(((Integer) cr.getId()).toString());
				if (cr.getOperationalStatus())
					statusTextArea.setText("Operational");
				else
					statusTextArea.setText("Non Operational");
				resourcesList.setListData((Vector<String>) toS(cr
						.getResources(), model.search.System.getAllRes()));
			}
		}

		private List<String> toS(HashMap<Resource, Integer> res,
				List<Resource> allRes) {
			// transform the dictionary into a list of strings
			List<String> ssr = new Vector<String>();

			for (int i = 0; i < allRes.size(); i++) {
				if (res.containsKey(allRes.get(i))) {
					String s = allRes.get(i).toString() + ": "
							+ res.get(allRes.get(i)).toString();
					ssr.add(s);
				}
			}
			if (ssr.isEmpty())
				ssr.add("There is no resources");

			return ssr;
		}

	}

}
